package com.pearadmin.hospital.controller;

import com.github.pagehelper.PageInfo;
import com.pearadmin.hospital.domain.HospitalOwinfo;
import com.pearadmin.common.tools.string.Convert;
import com.pearadmin.common.web.base.BaseController;
import com.pearadmin.common.web.domain.request.PageDomain;
import com.pearadmin.common.web.domain.response.Result;
import com.pearadmin.common.web.domain.response.module.ResultTable;
import com.pearadmin.common.tools.secure.SecurityUtil;
import com.pearadmin.system.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import com.pearadmin.hospital.service.IHospitalOwinfoService;

import java.time.LocalDateTime;

/**
 * 出入库Controller
 *
 * @author yuan
 * @date 2021-07-29
 */
@RestController
@RequestMapping("/hospital/owinfo")
public class HospitalOwinfoController extends BaseController
{
    private String prefix = "hospital/owinfo";

    @Autowired
    private IHospitalOwinfoService hospitalOwinfoService;

    @GetMapping("/main")
    @PreAuthorize("hasPermission('/hospital/owinfo/main','hospital:owinfo:main')")
    public ModelAndView main()
    {
        return jumpPage(prefix + "/main");
    }

    /**
     * 查询出入库列表
     */
    @ResponseBody
    @GetMapping("/data")
    @PreAuthorize("hasPermission('/hospital/owinfo/data','hospital:owinfo:data')")
    public ResultTable list(@ModelAttribute HospitalOwinfo hospitalOwinfo, PageDomain pageDomain)
    {
        PageInfo<HospitalOwinfo> pageInfo = hospitalOwinfoService.selectHospitalOwinfoPage(hospitalOwinfo,pageDomain);
        return pageTable(pageInfo.getList(),pageInfo.getTotal());
    }

    /**
     * 新增出入库
     */
    @GetMapping("/add")
    @PreAuthorize("hasPermission('/hospital/owinfo/add','hospital:owinfo:add')")
    public ModelAndView add()
    {
        return jumpPage(prefix + "/add");
    }

    /**
     * 新增保存出入库
     */
    @ResponseBody
    @PostMapping("/save")
    @PreAuthorize("hasPermission('/hospital/owinfo/add','hospital:owinfo:add')")
    public Result save(@RequestBody HospitalOwinfo hospitalOwinfo)
    {
        SysUser sysUser = (SysUser)SecurityUtil.currentUserObj();
        hospitalOwinfo.setCreateTime(LocalDateTime.now());
        hospitalOwinfo.setCreateBy(sysUser.getUserId());
        hospitalOwinfo.setCreateName(sysUser.getUsername());
        return decide(hospitalOwinfoService.insertHospitalOwinfo(hospitalOwinfo));
    }

    /**
     * 修改出入库
     */
    @GetMapping("/edit")
    @PreAuthorize("hasPermission('/hospital/owinfo/edit','hospital:owinfo:edit')")
    public ModelAndView edit(Integer id, ModelMap mmap)
    {
        HospitalOwinfo hospitalOwinfo = hospitalOwinfoService.selectHospitalOwinfoById(id);
        mmap.put("hospitalOwinfo", hospitalOwinfo);
        return jumpPage(prefix + "/edit");
    }

    /**
     * 修改保存出入库
     */
    @ResponseBody
    @PutMapping("/update")
    @PreAuthorize("hasPermission('/hospital/owinfo/edit','hospital:owinfo:edit')")
    public Result update(@RequestBody HospitalOwinfo hospitalOwinfo)
    {
        SysUser sysUser = (SysUser)SecurityUtil.currentUserObj();
        hospitalOwinfo.setUpdateTime(LocalDateTime.now());
        hospitalOwinfo.setUpdateBy(sysUser.getUserId());
        hospitalOwinfo.setUpdateName(sysUser.getUsername());
        return decide(hospitalOwinfoService.updateHospitalOwinfo(hospitalOwinfo));
    }

    /**
     * 删除出入库
     */
    @ResponseBody
    @DeleteMapping( "/batchRemove")
    @PreAuthorize("hasPermission('/hospital/owinfo/remove','hospital:owinfo:remove')")
    public Result batchRemove(String ids)
    {
        return decide(hospitalOwinfoService.deleteHospitalOwinfoByIds(Convert.toStrArray(ids)));
    }

    /**
     * 删除
     */
    @ResponseBody
    @DeleteMapping("/remove/{id}")
    @PreAuthorize("hasPermission('/hospital/owinfo/remove','hospital:owinfo:remove')")
    public Result remove(@PathVariable("id") Integer id)
    {
        return decide(hospitalOwinfoService.deleteHospitalOwinfoById(id));
    }
}
